package org.lxw.stock;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;
import java.util.SortedMap;

import org.lxw.stock.LC5Data.Key;

import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.StoreConfig;

/**
 * Hello world!
 * 
 */
public class ReadApp {
	private Environment env;
	final EntityStore store;
	final PrimaryIndex<Key, LC5Data> index;

	public ReadApp(File envHome) {
		EnvironmentConfig envConfig = new EnvironmentConfig();
		envConfig.setAllowCreate(true);
		envConfig.setTransactional(true);
		env = new Environment(envHome, envConfig);

		/* Open a transactional entity store. */
		StoreConfig storeConfig = new StoreConfig();
		storeConfig.setAllowCreate(true);
		storeConfig.setTransactional(false);
		store = new EntityStore(env, "LC5Store", storeConfig);
		index = store.getPrimaryIndex(LC5Data.Key.class, LC5Data.class);
	}

	private LC5Data getData(Key key) {
		return index.get(key);
	}

	public static void main(String[] args) throws IOException {
		ReadApp app = new ReadApp(new File("e:/stockdata"));
		SortedMap<Key, LC5Data> fd = app.index.sortedMap();
		for (Key key : fd.keySet()) {
			System.out.println(key.stock + " " + fd.get(key).toString());
		}
		app.close();
	}

	public boolean put(LC5Data data) {
		System.out.println(index.get(data.key));
		if (index.contains(data.key))
			return false;
		return index.putNoOverwrite(data);
	}

	public void close() {
		store.close();
	}

}
